// pages/twenty_four_math/twenty_four_math.js
const twentyFour = require('../../utils/twenty_four.js')
const app = getApp()
import {
  evaluate
} from '../../utils/eval.min.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    solution: 0,
    answer_list_left: [],
    answer_list_right: [],
    is_ratio: 0,
    test: [],
    is_idx_0: false, //选中卡片
    is_idx_1: false, //选中卡片
    is_idx_2: false, //选中卡片
    is_idx_3: false, //选中卡片
    is_last_symbol: false,
    is_last_number: false,
    last_pick: '',
    pick_math: '',
    pick_methods: [],
    is_show: false,
    number_list: [...Array.from({
      length: 13
    }, (i, idx) => idx + 1)]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let test = options.test
    if (test) {
      test = test.split(',')
      this.getAnswerList(test,()=>{
        this.setData({
          test:test
        })
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '算二十四点自定义题目',
      path: `/pages/twenty_four_math_diy/twenty_four_math_diy?test=${this.data.test}`
    }
  },
  /* 
  朋友圈
  */
  onShareTimeline() {
    return {
      query: `test=${this.data.test}`
    }
  },
  /* 生成题目 */
  setTest(e) {
    let list = this.data.test
    if (list.length < 4) {
      let {
        idx,
      } = e.currentTarget.dataset
      idx = +idx
      list.push(this.data.number_list[idx])
      if (list.length === 4) {
        this.setData({
          test: list
        })
        this.checkTest(this.data.test)
      } else {
        this.setData({
          test: list
        })
      }
    } else {
      wx.showToast({
        title: '只能选择4个数字',
        icon: 'none'
      })
      return
    }
  },
  /* 验证题目 */
  checkTest(test) {
    let is_test = twentyFour.main(test)
    if (is_test) {
      this.getAnswerList(test,()=>{})
    } else {
      wx.showToast({
        title: '本题无解',
        icon: 'none'
      })
      this.resetTest()
    }
  },
  /* 获取答案 */
  getAnswerList(test,cb) {
    let is_test = twentyFour.main(test)
    let answer_list = is_test.arr
    let list0 = []
    let list1 = []
    answer_list.forEach((item, idx) => {
      if (idx % 2) {
        list1.push(item)
      } else {
        list0.push(item)
      }
    })
    this.setData({
      answer_list_left: list0,
      answer_list_right: list1,
      solution: is_test.arr.length,
      is_ratio: is_test.ratio
    })
    cb()
  },
  /* 重置题目 */
  resetTest() {
    this.setData({
      solution: 0,
      answer_list_left: [],
      answer_list_right: [],
      is_ratio: 0,
      test: [],
      is_idx_0: false, //选中卡片
      is_idx_1: false, //选中卡片
      is_idx_2: false, //选中卡片
      is_idx_3: false, //选中卡片
      is_last_symbol: false,
      is_last_number: false,
      last_pick: '',
      pick_math: '',
      pick_methods: [],
    })
  },
  /* 输入数字 */
  clickCard(e) {
    if (this.data.test.length != 4) {
      wx.showToast({
        title: '题目未成立',
        icon: 'none'
      })
      return
    }
    if (this.data.is_last_number) {
      wx.vibrateLong({})
      return
    }
    let {
      idx,
    } = e.currentTarget.dataset
    idx = +idx
    switch (idx) {
      case 0:
        if (!this.data.is_idx_0) {
          this.setData({
            is_idx_0: true,
            is_last_symbol: false,
            is_last_number: true,
            pick_math: this.data.pick_math + this.data.test[idx],
            pick_methods: [...this.data.pick_methods, idx],
          })
        }
        break
      case 1:
        if (!this.data.is_idx_1) {
          this.setData({
            is_idx_1: true,
            is_last_symbol: false,
            is_last_number: true,
            pick_math: this.data.pick_math + this.data.test[idx],
            pick_methods: [...this.data.pick_methods, idx],
          })
        }
        break
      case 2:
        if (!this.data.is_idx_2) {
          this.setData({
            is_idx_2: true,
            is_last_symbol: false,
            is_last_number: true,
            pick_math: this.data.pick_math + this.data.test[idx],
            pick_methods: [...this.data.pick_methods, idx],
          })
        }
        break
      case 3:
        if (!this.data.is_idx_3) {
          this.setData({
            is_idx_3: true,
            is_last_symbol: false,
            is_last_number: true,
            pick_math: this.data.pick_math + this.data.test[idx],
            pick_methods: [...this.data.pick_methods, idx],
          })
        }
        break
      default:
        break
    }
  },
  /* 输入符号 */
  clickMathCard(e) {
    if (this.data.test.length != 4) {
      wx.showToast({
        title: '题目未成立',
        icon: 'none'
      })
      return
    }
    let {
      idx,
    } = e.currentTarget.dataset
    if (idx == "21" || idx == "22") {
      this.setData({
        is_last_symbol: false,
      })
    } else {
      if (this.data.is_last_symbol || !this.data.is_last_number) {
        wx.vibrateLong({})
        return
      }
      this.setData({
        is_last_symbol: true,
      })
    }
    let is_idx
    switch (idx) {
      case '11':
        is_idx = '+'
        break
      case '12':
        is_idx = '-'
        break
      case '13':
        is_idx = '×'
        break
      case '14':
        is_idx = '÷'
        break
      case '21':
        is_idx = '('
        break
      case '22':
        is_idx = ')'
        break
      default:
        break
    }
    this.setData({
      is_last_number: false,
      pick_math: this.data.pick_math + is_idx,
      pick_methods: [...this.data.pick_methods, idx],
    })
  },
  /* 后退 */
  clickBackSpace() {
    if (this.data.test.length != 4) {
      wx.showToast({
        title: '题目未成立',
        icon: 'none'
      })
      return
    }
    let is_methods = this.data.pick_methods
    let is_pop = is_methods.pop()
    let is_length = is_methods.length
    let is_last
    if (is_length) {
      let is_pick = this.pickLabel(is_methods)
      is_last = is_methods[is_length - 1]
      console.log(is_last)
      switch (+is_last) {
        case 0:
        case 1:
        case 2:
        case 3:
          this.setData({
            is_last_symbol: false,
            is_last_number: true,
          })
          break
        case 21:
        case 22:
          this.setData({
            is_last_symbol: false,
            is_last_number: false,
          })
          break
        case 11:
        case 12:
        case 13:
        case 14:
          this.setData({
            is_last_symbol: true,
            is_last_number: false,
          })
          break
        default:
          break
      }
      this.setData({
        last_pick: is_last,
        pick_math: is_pick
      })
    } else {
      this.setData({
        last_pick: '',
        pick_math: '',
        is_last_symbol: false,
        is_last_number: false,
      })
    }
    this.setData({
      pick_methods: is_methods
    })
    switch (+is_pop) {
      case 0:
        this.setData({
          is_idx_0: false,
        })
        break
      case 1:
        this.setData({
          is_idx_1: false,
        })
        break
      case 2:
        this.setData({
          is_idx_2: false,
        })
        break
      case 3:
        this.setData({
          is_idx_3: false,
        })
        break
      default:
        break
    }
  },
  /* 验证答案 */
  clickConfirm() {
    if (this.data.test.length != 4) {
      wx.showToast({
        title: '题目未成立',
        icon: 'none'
      })
      return
    }
    if (!this.data.is_idx_0 || !this.data.is_idx_1 || !this.data.is_idx_2 || !this.data.is_idx_3) {
      wx.showToast({
        title: '数字卡片需用完!',
        icon: 'none'
      })
      wx.vibrateLong({})
      return
    }
    let is_pick = this.data.pick_math.toString()
    is_pick = is_pick.replace(/×/g, "*").replace(/÷/g, "/")
    console.log(evaluate(is_pick))
    let is_answer = evaluate(is_pick) === 24
    // console.log(is_answer)
    if (is_answer) {
      wx.showToast({
        title: '计算成功',
        icon: 'seccess'
      })
      this.resetTest()
    } else {
      wx.showToast({
        title: '计算失败',
        icon: 'none'
      })
      wx.vibrateLong({})
    }
  },
  /* 查看答案 */
  clickAnswerList() {
    if (this.data.test.length != 4) {
      wx.showToast({
        title: '题目未成立',
        icon: 'none'
      })
      return
    }
    this.setData({
      is_show: true
    })
  },
  /* 关闭答案 */
  closeShow() {
    this.setData({
      is_show: false
    })
  },
  /* 答案转换 */
  pickLabel(array) {
    let is_methods = array
    let is_pick = ''
    for (let i = 0; i < is_methods.length; i++) {
      let label = is_methods[i]
      switch (+is_methods[i]) {
        case 0:
          label = this.data.test[0]
          break
        case 1:
          label = this.data.test[1]
          break
        case 2:
          label = this.data.test[2]
          break
        case 3:
          label = this.data.test[3]
          break
        case 11:
          label = '+'
          break
        case 12:
          label = '-'
          break
        case 13:
          label = '×'
          break
        case 14:
          label = '÷'
          break
        case 21:
          label = '('
          break
        case 22:
          label = ')'
          break
        default:
          break
      }
      is_pick += label
    }
    return is_pick
  }
})